import { createRouter, createWebHistory} from 'vue-router'
import type { RouteRecordRaw} from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { STATIC_ROUTES } from './routers'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [...STATIC_ROUTES]
})

const asyncImportView = async (filename:string)  =>  {
  return await import(/* @vite-ignore */`@app/views/${filename}`);
}

router.beforeEach(async (to, from, next) => {
  NProgress.configure({ showSpinner: false })
  NProgress.start()
  
  let matched: any = to.matched;

  //console.log({to, from, matched:JSON.parse(JSON.stringify(matched))},to.redirectedFrom && to.redirectedFrom._count,'路由跳转!');

  if(matched && matched.length && matched[matched.length-1].path == '/:pathMatch(.*)*'){
    if(to.redirectedFrom){
      next('/404')
      return;
    }
    next({ path: to.path, query: to.query })
    return;
  }

  next()
})

// 全局后置钩子
router.afterEach(() => {
  NProgress.done()
})

console.log('router', '初始路由');

export default router
